/**
 * Created by IntelliJ IDEA.
 * User: GuoDapeng
 * Date: 16/4/21
 * Time: 下午4:58
 */

/**
 * 猜测实收
 * @param s
 * @returns {*}
 */
function speculatePaid(s) {
    var numberStr = s + "";
    //var numberLength = numberStr.length;
    var array = numberStr.split(""); //字符分割;

    var list = [];

    if(s < 10){
        list = [1,5,10,20,50,100];
        for(var i = 0;i < list.length;i++){
            if(list[i]< s){
                list[i]= 0;
            }
        }
        list = unique(list);
        removeByValue(list,0);
        return list;
    }

    if(array.length == 1){
        array = ["0","0",array[0]];
    }

    if(array.length == 2){
        array = ["0",array[0],array[1]];
    }



    var tem = JSON.stringify(array);
    tem = JSON.parse(tem);
    tem[tem.length-2] = "1";
    list[list.length] = tem.join("")*1;
    tem[tem.length-1] = "0";
    list[list.length] = tem.join("")*1;

    tem = JSON.stringify(array);
    tem = JSON.parse(tem);
    tem[tem.length-2] = "2";
    list[list.length] = tem.join("")*1;
    tem[tem.length-1] = "0";
    list[list.length] = tem.join("")*1;

    tem = JSON.stringify(array);
    tem = JSON.parse(tem);
    if(tem[tem.length-2] == 2 && tem[tem.length-1]>0){
        tem[tem.length-2] = "3";
        tem[tem.length-1] = "0";
        list[list.length] = tem.join("")*1;
    }

    tem = JSON.stringify(array);
    tem = JSON.parse(tem);
    tem[tem.length-2] = "5";
    list[list.length] = tem.join("")*1;
    tem[tem.length-1] = "0";
    list[list.length] = tem.join("")*1;

    tem = JSON.stringify(array);
    tem = JSON.parse(tem);
    if(tem[tem.length-2] == 5){
        tem[tem.length-2] = "6";
        list[list.length] = tem.join("")*1;
    }

    tem = JSON.stringify(array);
    tem = JSON.parse(tem);
    if(tem[tem.length-2] != 5 &&
        tem[tem.length-2] != 4 &&
        tem[tem.length-2] != 3 &&
        tem[tem.length-2] != 2 &&
        tem[tem.length-2] != 1){
        tem[tem.length-2] = "7";
        list[list.length] = tem.join("")*1;
        tem[tem.length-1] = "0";
        list[list.length] = tem.join("")*1;
    }

    tem = JSON.stringify(array);
    tem = JSON.parse(tem);
    if(tem[tem.length-2] != 6 &&
        tem[tem.length-2] != 5 &&
        tem[tem.length-2] != 4 &&
        tem[tem.length-2] != 3 &&
        tem[tem.length-2] != 2 &&
        tem[tem.length-2] != 1){
        tem[tem.length-2] = "8";
        list[list.length] = tem.join("")*1;
        tem[tem.length-1] = "0";
        list[list.length] = tem.join("")*1;
    }

    tem = JSON.stringify(array);
    tem = JSON.parse(tem);
    tem[tem.length-2] = "0";
    tem[tem.length-3] = (tem[tem.length-3]*1 + 1) + "";
    list[list.length] = tem.join("")*1;
    tem[tem.length-1] = "0";
    list[list.length] = tem.join("")*1;

    tem = JSON.stringify(array);
    tem = JSON.parse(tem);
    //var hundreds = tem[tem.length-3];
    var ten = tem[tem.length-2];
    var bits = tem[tem.length-1];

    switch(true)
    {
        case ten >= 1 && ten < 2:
            tem[tem.length-2] = "2";
            list[list.length] = tem.join("")*1;
            break;
        case ten >= 2 && ten < 3:
            break;
        case ten >= 3 && ten < 4:
            tem[tem.length-2] = "4";
            list[list.length] = tem.join("")*1;
            tem[tem.length-2] = "5";
            list[list.length] = tem.join("")*1;
            break;
        case ten >= 4 && ten < 5:
            tem[tem.length-2] = "5";
            list[list.length] = tem.join("")*1;
            break;
        case ten >= 5 && ten < 6:
            break;
        case ten >= 6 && ten < 7:
            tem[tem.length-2] = "7";
            list[list.length] = tem.join("")*1;
            tem[tem.length-2] = "1";
            tem[tem.length-3] = (tem[tem.length-3]*1+1)+"";
            list[list.length] = tem.join("")*1;
            break;
        case ten >= 7 && ten < 8:
            tem[tem.length-2] = "2";
            tem[tem.length-3] = (tem[tem.length-3]*1+1)+"";
            list[list.length] = tem.join("")*1;
            break;
        case ten >= 8 && ten < 9:
            tem[tem.length-2] = "9";
            list[list.length] = tem.join("")*1;
            tem[tem.length-2] = "9";
            tem[tem.length-3] = (tem[tem.length-3]*1+1)+"";
            list[list.length] = tem.join("")*1;
            break;
        default:
    }

    for(i = 0;i < list.length;i++){
        if(list[i]< s){
            list[i]= 0;
        }
    }

    if(s%100 > 0 && s%100 < 10){
        for(i = 0;i < list.length;i++){
            numberStr = list[i] + "";
            tem = numberStr.split(""); //字符分割;

            //hundreds = tem[tem.length-3];
            ten = tem[tem.length-2];
            bits = tem[tem.length-1];

            if(bits > 0 && (ten == 2 || ten ==5 || ten == 7 || ten == 8 || ten == 0)){
                list[i]=0;
            }
            if(bits == 0 && (ten == 7 || ten ==8)){
                list[i]=0;
            }
        }
    }

    list = unique(list);
    removeByValue(list,0);

    return list;
}

function unique(arr) {
    var result = [], hash = {};
    for (var i = 0, elem; (elem = arr[i]) != null; i++) {
        if (!hash[elem]) {
            result.push(elem);
            hash[elem] = true;
        }
    }
    return result;
}

function removeByValue(arr, val) {
    for(var i=0; i<arr.length; i++) {
        if(arr[i] == val) {
            arr.splice(i, 1);
            break;
        }
    }
}
